import { PlusOutlined } from '@ant-design/icons';
import { UploadOutlined } from '@ant-design/icons';
import { Divider,Button, Upload, message } from 'antd';
import React, { useState, useRef } from 'react';
import { PageContainer } from '@ant-design/pro-layout';
import ProTable, { ProColumns, ActionType } from '@ant-design/pro-table';
import orderList from '../func/order_list';
import type { UploadProps } from 'antd';
import { getAccessToken } from '@/utils/authority'; 
import CreateForm from './createorder/createform';
import OrderProductForm from './orderview/orderproductForm';
import UpdateForm from './updateform/updateform';
import AddorderproductForm from './addorderproduct/addorderproduct';
import { OrderTableListItem } from './data';
import orderCreate from '../func/order_create';


const TableList: React.FC<{}> = () => {
  const actionRef = useRef<ActionType>();
  const [createModalVisible, handleCreateModalVisible] = useState<boolean>(false);
  const [updateModalVisible, handleUpdateModalVisible] = useState<boolean>(false);
  const [addModalVisible, handleAddModalVisible] = useState<boolean>(false);
  const [stepFormValues, setStepFormValues] = useState({});
  const [orderproductModalVisible, handleorderproductModalVisible] = useState<boolean>(false);
  const [orderkey, setorderkey] = useState<any>();
  const props: UploadProps = {
    name: 'file',
    action: 'http://192.168.2.32:21040/api/order/fileupload/',
    // action: 'http://localhost:21040/api/order/fileupload/',
    headers: {
      'X-Access-Token': getAccessToken(),
    },
    onChange(info) {
      if (info.file.status === 'done') {
        console.log(info.file.response.code)
        if (info.file.response.code==200){
        message.success(`${info.file.name}上传成功`);
        }else{
          message.error(info.file.response.msg)
        }
      } else if (info.file.status === 'error') {
        message.error(`${info.file.name}上传失败`);
      }
    },
  };
  const columns: ProColumns<OrderTableListItem>[] = [
    {
      title: "订单号",
      dataIndex: 'order_id',
      valueType: 'text',
    },
    {
      title: "联络人",
      dataIndex: 'custom_name',
      valueType: 'text',
    },
    {
      title: "业务编号",
      dataIndex: 'custom_name_number',
      valueType: 'text',
    },
    {
      title: "客户公司名称",
      dataIndex: 'custom_company',
      valueType: 'text',
    },
    {
      title: "客户公司编号",
      dataIndex: 'custom_company_number',
      valueType: 'text',
    },
    {
      title: "联系电话",
      dataIndex: 'custom_phone',
      valueType: 'text',
    },
    {
      title: "订单日期",
      dataIndex: 'order_date',
      valueType: 'date',
      search:false,
    },
    {
      title: "开始时间",
      dataIndex: 'start_date',
      valueType: 'date',
      hideInTable:true,
      hideInForm:true,
    },
    {
      title: "结束时间",
      dataIndex: 'end_date',
      valueType: 'date',
      hideInTable:true,
      hideInForm:true,
    },
    {
      title: "操作",
      dataIndex: 'option',
      valueType: 'option',
      render: (_, record) => (
        <>
          <a
            onClick={() => {
              setorderkey(record.id);
              handleorderproductModalVisible(true);
            }}
          >
            查看订单详情
          </a>
          <Divider type="vertical" />
          <a
            onClick={() => {
              setorderkey(record.id);
              setStepFormValues(record);
              handleUpdateModalVisible(true);
            }}
          >
            修改订单
          </a>
          <Divider type="vertical" />
          <a
            onClick={() => {
              setorderkey(record.id);
              handleAddModalVisible(true);
            }}
          >
            添加订单产品
          </a>
        </>
      ),
    },
  ];

  return (
    <PageContainer>
      <ProTable<OrderTableListItem>
        headerTitle={"查询最新版本"}
        actionRef={actionRef}
        rowKey="key"
        search={{
          labelWidth: 120,
        }}
        toolBarRender={() => [
          <Upload {...props}>
            <Button icon={<UploadOutlined />}>导入订单</Button>
          </Upload>,
          <Button type="primary" key="primary" onClick={() => handleCreateModalVisible(true)}>
            <PlusOutlined /> 手动添加
          </Button>,
        ]}
        request={(params, sorter, filter) => orderList({ ...params, sorter, filter })}
        columns={columns}
      />
      <CreateForm onCancel={() => handleCreateModalVisible(false)} modalVisible={createModalVisible}>
        <ProTable<OrderTableListItem, OrderTableListItem>
          onSubmit={async (value) => {
            const success = await orderCreate(value);
            if (success) {
              handleCreateModalVisible(false);
              if (actionRef.current) {
                actionRef.current.reload();
              }
            }
          }}
          rowKey="key"
          type="form"
          columns={columns}
        />
      </CreateForm>
      <OrderProductForm
        onCancel={() => {
          handleorderproductModalVisible(false);
        }}
        orderproductModalVisible={orderproductModalVisible}
        orderkey={orderkey}
      />
      <UpdateForm
        onCancel={() => {
          handleUpdateModalVisible(false);
          actionRef.current?.reload();
        }}
        modalVisible={updateModalVisible}
        orderkey={orderkey}
        values={stepFormValues}
      />
      <AddorderproductForm
        onCancel={() => {
          handleAddModalVisible(false);
          actionRef.current?.reload();
        }}
        modalVisible={addModalVisible}
        orderkey={orderkey}
      />
    </PageContainer>
  );
};

export default TableList;
